package com.yunxi.service.iam.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.iam.OrganizeVO;
import com.yunxi.eai.pojo.iam.PositionVO;
import com.yunxi.eai.pojo.iam.StaffVO;
import com.yunxi.eai.service.iam.IamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/23.
 */
/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class IamServiceImpl implements IamService {
    private static final Logger logger = LoggerFactory.getLogger(IamServiceImpl.class);

    @Override
    public Object findData(EsbVO esbVO) {
        String jsonStr = esbVO.getReqBody();
        String serviceName = esbVO.getServiceName();
        String sourceSystem = esbVO.getSourceSystem();
        String url = esbVO.getUrl();
        String response = "";
        try {
            Map map = HttpUtil.sendPost(jsonStr, serviceName, sourceSystem, url);
            response=String.valueOf(map.get("response"));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            // jsonObject.get("statusFlag");
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){
                return ResponseUtil.fail(500, "从IAM获取数据失败!");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return ResponseUtil.fail(500, "从IAM获取数据失败!");
        }
        JSONObject jsonObject = JSON.parseObject(response);
        // System.out.println(jsonObject.get("Total"));
        Object data = jsonObject.get("Data");
        Object json = JSONObject.toJSON(data);
        if (serviceName.equals(Constant.IAM_ORG)) {
            ArrayList<OrganizeVO> orgList = JSON.parseObject(json.toString(), new TypeReference<ArrayList<OrganizeVO>>() {
            });
            //调用组织结构接口

        } else if (serviceName.equals(Constant.IAM_USER)) {
            ArrayList<StaffVO> staffList = JSON.parseObject(json.toString(), new TypeReference<ArrayList<StaffVO>>() {
            });
            //调用人员信息接口

        } else if (serviceName.equals(Constant.IAM_POST)) {
            ArrayList<PositionVO> postList = JSON.parseObject(json.toString(), new TypeReference<ArrayList<PositionVO>>() {
            });
            //调用职务信息接口

        }
        return ResponseUtil.ok("数据处理成功!");
    }
}
